package com.ruoyi.erecord.employee.service.impl;

import java.io.InputStream;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.tools.constant.ERecordConstant;
import com.ruoyi.common.tools.excel.base.MyExcelImportUtil;
import com.ruoyi.common.tools.excel.base.MyExcelResult;
import com.ruoyi.common.tools.excel.listener.EmployeeExcelListener;
import com.ruoyi.common.tools.utils.SerialGenerator;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.erecord.employee.domain.EmployeeImportBase;
import com.ruoyi.erecord.employee.domain.bo.EmployeeImportBaseBo;
import com.ruoyi.erecord.employee.domain.bo.EmployeeImportDetailBo;
import com.ruoyi.erecord.employee.domain.vo.EmployeeImportBaseVo;
import com.ruoyi.erecord.employee.domain.vo.EmployeeImportDetailVo;
import com.ruoyi.erecord.employee.mapper.EmployeeImportBaseMapper;
import com.ruoyi.erecord.employee.service.IEmployeeImportBaseService;
import com.ruoyi.erecord.employee.service.IEmployeeImportDetailService;
import com.ruoyi.oss.core.OssClient;
import com.ruoyi.oss.factory.OssFactory;
import com.ruoyi.system.domain.vo.SysOssVo;
import com.ruoyi.system.service.ISysOssService;

import cn.hutool.core.bean.BeanUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 人员导入主Service业务层处理
 *
 * @author cc
 * @date 2023-04-14
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class EmployeeImportBaseServiceImpl implements IEmployeeImportBaseService {

	private final EmployeeImportBaseMapper baseMapper;

	private final ISysOssService sysOssService;

	private final IEmployeeImportDetailService employeeImportDetailService;
	
	/**
	 * 查询人员导入主
	 */
	@Override
	public EmployeeImportBaseVo queryById(String batchNo) {
		return baseMapper.selectVoById(batchNo);
	}

	/**
	 * 查询人员导入主列表
	 */
	@Override
	public TableDataInfo<EmployeeImportBaseVo> queryPageList(EmployeeImportBaseBo bo, PageQuery pageQuery) {
		LambdaQueryWrapper<EmployeeImportBase> lqw = buildQueryWrapper(bo);
		Page<EmployeeImportBaseVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
		return TableDataInfo.build(result);
	}

	/**
	 * 查询人员导入主列表
	 */
	@Override
	public List<EmployeeImportBaseVo> queryList(EmployeeImportBaseBo bo) {
		LambdaQueryWrapper<EmployeeImportBase> lqw = buildQueryWrapper(bo);
		return baseMapper.selectVoList(lqw);
	}

	private LambdaQueryWrapper<EmployeeImportBase> buildQueryWrapper(EmployeeImportBaseBo bo) {
		Map<String, Object> params = bo.getParams();
		LambdaQueryWrapper<EmployeeImportBase> lqw = Wrappers.lambdaQuery();
		lqw.eq(StringUtils.isNotBlank(bo.getBatchNo()), EmployeeImportBase::getBatchNo, bo.getBatchNo());
		lqw.eq(bo.getProcessStatus() != null, EmployeeImportBase::getProcessStatus, bo.getProcessStatus());
		lqw.eq(StringUtils.isNotBlank(bo.getProcessMsg()), EmployeeImportBase::getProcessMsg, bo.getProcessMsg());
		lqw.eq(bo.getAttachMsg() != null, EmployeeImportBase::getAttachMsg, bo.getAttachMsg());
		lqw.eq(StringUtils.isNotBlank(bo.getExcelUrl()), EmployeeImportBase::getExcelUrl, bo.getExcelUrl());
		return lqw;
	}

	/**
	 * 新增人员导入主
	 */
	@Override
	public Boolean insertByBo(EmployeeImportBaseBo bo) {
		EmployeeImportBase add = BeanUtil.toBean(bo, EmployeeImportBase.class);
		validEntityBeforeSave(add);
		boolean flag = baseMapper.insert(add) > 0;
		if (flag) {
			bo.setBatchNo(add.getBatchNo());
		}
		return flag;
	}

	/**
	 * 修改人员导入主
	 */
	@Override
	public Boolean updateByBo(EmployeeImportBaseBo bo) {
		EmployeeImportBase update = BeanUtil.toBean(bo, EmployeeImportBase.class);
		validEntityBeforeSave(update);
		return baseMapper.updateById(update) > 0;
	}

	/**
	 * 保存前的数据校验
	 */
	private void validEntityBeforeSave(EmployeeImportBase entity) {
		// TODO 做一些数据校验,如唯一约束
	}

	/**
	 * 批量删除人员导入主
	 */
	@Override
	public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
		if (isValid) {
			// TODO 做一些业务上的校验,判断是否需要校验
		}
		return baseMapper.deleteBatchIds(ids) > 0;
	}

}
